package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.ExamScoreMapper;
import com.bjfu.gmms.model.ExamScore;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/submitSingleExamScore")
public class SubmitExamScoresServlet extends HttpServlet {

    private ExamScoreMapper examScoreMapper;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SqlSession sqlSession = MyBatisUtil.getSqlSession("development");
        examScoreMapper=sqlSession.getMapper(ExamScoreMapper.class);
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        int examId = Integer.parseInt(request.getParameter("examId"));
        String scoreStr = request.getParameter("score");

        // 检查成绩是否为空，避免空值提交
        if (scoreStr == null || scoreStr.trim().isEmpty()) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("成绩不能为空");
            return;
        }
        try {
            double score = Double.parseDouble(scoreStr);
            ExamScore examScore = new ExamScore();
            examScore.setStudentId(studentId);
            examScore.setExamId(examId);
            examScore.setScore(BigDecimal.valueOf(score));
            // 调用存储过程插入或更新成绩
            examScoreMapper.insertOrUpdateScore(examScore);
            sqlSession.commit();
            response.getWriter().write("成绩提交成功");

        } catch (NumberFormatException e) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("成绩必须是有效的数字");
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("成绩提交失败");
            e.printStackTrace();
        }



    }

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        SqlSession sqlSession = MyBatisUtil.getSqlSession("development");
//        examScoreMapper=sqlSession.getMapper(ExamScoreMapper.class);
//        int studentId = Integer.parseInt(request.getParameter("studentId"));
//
//        // 遍历所有的成绩字段，并调用存储过程
//        for (String param : request.getParameterMap().keySet()) {
//            if (param.startsWith("score_")) {
//                int examId = Integer.parseInt(request.getParameter("examId_" + param.split("_")[1]));
//                if(request.getParameter(param)==null) continue;
//                double score = Double.parseDouble(request.getParameter(param));
//                ExamScore examScore = new ExamScore();
//                examScore.setStudentId(studentId);
//                examScore.setExamId(examId);
//                examScore.setScore(BigDecimal.valueOf(score));
//                // 调用存储过程录入成绩
//                examScoreMapper.insertOrUpdateScore(examScore);
//            }
//        }
//            sqlSession.commit();
//            response.sendRedirect("businessTwo.jsp"); // 跳转到成功页面
//
//    }
}
